package org.dkakunsi.aplikasitoko.form;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JButton;

import org.dkakunsi.aplikasitoko.data.entity.Log;
import org.dkakunsi.aplikasitoko.data.entity.Pelaku;
import org.dkakunsi.aplikasitoko.data.util.ApplicationDataException;
import org.dkakunsi.aplikasitoko.form.admin.DataBarang;
import org.dkakunsi.aplikasitoko.form.admin.DataKasir;
import org.dkakunsi.aplikasitoko.form.admin.DataSupplier;
import org.dkakunsi.aplikasitoko.form.admin.Laporan;
import org.dkakunsi.aplikasitoko.form.gudang.Gudang;
import org.dkakunsi.aplikasitoko.form.kasir.Password;
import org.dkakunsi.aplikasitoko.form.kasir.Penjualan;
import org.dkakunsi.aplikasitoko.service.DataService;

public class Desktop extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	private JDesktopPane desktopPane;
	private JToolBar toolBar;
	private JButton btnBtn_1;
	private JButton btnBtn_2;
	private JButton btnBtn_3;
	private JButton btnBtn_4;
	private JMenuBar menuBar;
	private JMenu mnFile;
	private JMenuItem mntmLogOut;
	private Pelaku pelaku;
	private String tipeUser;


	/**
	 * Create the frame.
	 */
	public Desktop(String type, Pelaku pelaku) {
		super("APLIKASI TOKO");
		setPelaku(pelaku);
		setUndecorated(true);
        setSize((Toolkit.getDefaultToolkit().getScreenSize().width),(Toolkit.getDefaultToolkit().getScreenSize().height-30));
        setLocation(0,0);

        setter();
        tipeUser = type;
		switch(type){
			case "ADMIN" : setAdmin();
				break;
			case "KASIR" : setKasir();
				break;
			case "GUDANG" : setGudang();
				break;
			default :
				break;
		}
	}
	
	private void setter(){
		contentPane = new JPanel();
		desktopPane = new JDesktopPane();
		toolBar = new JToolBar();
		btnBtn_1 = new JButton("");
		btnBtn_2 = new JButton("");
		btnBtn_3 = new JButton("");
		btnBtn_4 = new JButton("");
		menuBar = new JMenuBar();
		mnFile = new JMenu("File");
		mntmLogOut = new JMenuItem("Log Out");

		setJMenuBar(menuBar);
		menuBar.add(mnFile);
		mnFile.add(mntmLogOut);

		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		contentPane.add(desktopPane, BorderLayout.CENTER);		
		contentPane.add(toolBar, BorderLayout.NORTH);
		
		toolBar.add(btnBtn_1);
		toolBar.add(btnBtn_2);
		toolBar.add(btnBtn_3);
		toolBar.add(btnBtn_4);
		
		btnBtn_1.addActionListener(this);
		btnBtn_2.addActionListener(this);
		btnBtn_3.addActionListener(this);
		btnBtn_4.addActionListener(this);
		mntmLogOut.addActionListener(this);
		
		mntmLogOut.setActionCommand("LOGOUT");
	}
	
	private void setAdmin(){
		btnBtn_1.setToolTipText("Data Barang");
		btnBtn_1.setActionCommand("DATABARANG");
		ImageIcon icon_1 = new ImageIcon( getClass().getResource( "/icon/Barang.png") );
		btnBtn_1.setIcon(getIcon(icon_1));

		btnBtn_2.setToolTipText("Data Kasir");
		btnBtn_2.setActionCommand("DATAKASIR");
		ImageIcon icon_2 = new ImageIcon( getClass().getResource( "/icon/Kasir.png") );
		btnBtn_2.setIcon(getIcon(icon_2));

		btnBtn_3.setToolTipText("Data Supplier");
		btnBtn_3.setActionCommand("DATASUPPLIER");
		ImageIcon icon_3 = new ImageIcon( getClass().getResource( "/icon/Supplier.png") );
		btnBtn_3.setIcon(getIcon(icon_3));

		btnBtn_4.setToolTipText("Laporan");
		btnBtn_4.setActionCommand("LAPORAN");
		ImageIcon icon_4 = new ImageIcon( getClass().getResource( "/icon/Report.png") );
		btnBtn_4.setIcon(getIcon(icon_4));
	}

	private void setKasir(){
		btnBtn_1.setToolTipText("Aplikasi Penjualan");
		btnBtn_1.setActionCommand("PENJUALAN");
		ImageIcon icon_1 = new ImageIcon( getClass().getResource( "/icon/Penjualan.png") );
		btnBtn_1.setIcon(getIcon(icon_1));

		btnBtn_2.setToolTipText("Password");
		btnBtn_2.setActionCommand("PASSWORD");
		ImageIcon icon_2 = new ImageIcon( getClass().getResource( "/icon/Password.png") );
		btnBtn_2.setIcon(getIcon(icon_2));
		
		btnBtn_3.setVisible(false);
		btnBtn_3.setActionCommand("");
		
		btnBtn_4.setVisible(false);
		btnBtn_4.setActionCommand("");
	}

	private void setGudang(){
		btnBtn_1.setToolTipText("Aplikasi Pembelian");
		btnBtn_1.setActionCommand("PEMBELIAN");
		ImageIcon icon_1 = new ImageIcon( getClass().getResource( "/icon/Gudang.png") );
		btnBtn_1.setIcon(getIcon(icon_1));

		btnBtn_2.setVisible(false);
		btnBtn_2.setActionCommand("");
		
		btnBtn_3.setVisible(false);
		btnBtn_3.setActionCommand("");
		
		btnBtn_4.setVisible(false);
		btnBtn_4.setActionCommand("");
	}
	
	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e){
		String command = e.getActionCommand();
		MyForm form = null;
		
		switch(command){
		case "DATABARANG" :
			form = new DataBarang("DATA BARANG");
			desktopPane.add(form );
			form.show();
			break;
		case "DATAKASIR" :
			form = new DataKasir("DATA KASIR");
			desktopPane.add(form );
			form .show();
			break;
		case "DATASUPPLIER" :
			form = new DataSupplier("DATA SUPPLIER");
			desktopPane.add(form );
			form.show();
			break;
		case "LAPORAN" :
			Laporan lap = new Laporan();
			desktopPane.add( lap );
			lap.show();
			break;
		case "PENJUALAN" :
			form = new Penjualan("PENJUALAN", this);
			desktopPane.add(form );
			form.show();
			break;
		case "PASSWORD" :
			form = new Password("PASSWORD", this);
			desktopPane.add(form );
			form.show();
			break;
		case "PEMBELIAN" :
			form = new Gudang("PEMBELIAN");
			desktopPane.add(form );
			form.show();
			break;
		case "LOGOUT" :
			// save session to Log
			if( tipeUser.equals( "KASIR" ) ) {
				Log log = (Log) pelaku;
				log.setJamKeluar("15:00");
				DataService ds = new DataService();
				try {
					ds.update(pelaku);
				} catch (ApplicationDataException e1) {
					JOptionPane.showMessageDialog(this, "Call the Developer", "Application Error", JOptionPane.ERROR_MESSAGE);
//					e1.printStackTrace();
				}
			}
			dispose();
			new Login().show(true);
			break;
		default :
				break;
		}
	}

	private ImageIcon getIcon(ImageIcon icon){
        Image img = icon.getImage();
        BufferedImage bi = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.createGraphics();
        g.drawImage(img, 0, 0, 50, 50, null);
        ImageIcon newIcon = new ImageIcon(bi);

		return newIcon;
	}
	
	/**
	 * @return the log
	 */
	public Pelaku getPelaku() {
		return pelaku;
	}

	/**
	 * @param log the log to set
	 */
	private void setPelaku(Pelaku pelaku) {
		this.pelaku = pelaku;
	}
	
	public JDesktopPane getDesktopPane() {
		return this.desktopPane;
	}
}
